/**
 * 
 */
package controller.xml;

import model.preprocessor.PreProcessorResult;
import model.preprocessor.PreProcessorResultStatus;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

/**
 * Use to parse a {@link PreProcessorResult} in the exported session in XML.
 * 
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public class PreProcessorResultXMLHandler extends XMLHandler {

	private PreProcessorResult			result	= null;
	private PreProcessorResultXMLStatus	status	= PreProcessorResultXMLStatus.NONE;

	// private BasicResultXMLHandler basicHandler = null;

	public PreProcessorResultXMLHandler() {
	}


	@Override
	public boolean characters2(char[] ch, int start, int length) throws SAXException {

		switch (status) {
		case NONE:
		case BASIC_RESULT:
			throw new SAXException("not supposed to receive character in this status");
		default:
			return super.characters2(ch, start, length);
		}
	}

	@Override
	public boolean endDocument2() throws SAXException {
		return false;
	}

	@Override
	public boolean endElement2(String uri, String localName, String qName) throws SAXException {
		switch (status) {
		case NONE:
			return false;

		case BASIC_RESULT:
			if (qName.equals(XMLBaliseFormat.BASIC_RESULT))
			{// ferme basic result
				status = PreProcessorResultXMLStatus.NONE;
				result = new PreProcessorResult(((BasicResultXMLHandler) delegate).getBasic());
				delegate = null;
				return true;
			}
			throw new SAXException("end of BASICRESULT was expected");

		case PREPROC_RESULT_STATUS:
			if (qName.equals(XMLBaliseFormat.PREPROC_RESULT_STATUS))
			{// ferme status
				setResultStatus();
				status = PreProcessorResultXMLStatus.NONE;
				return true;
			}
			throw new SAXException("end of STATUS was expected");
		}

		// attend une fin de balise qui existe pas...
		throw new SAXException("an other end of element was expected");
	}

	@Override
	public boolean startElement2(String arg0, String arg1, String arg2, Attributes arg3) throws SAXException {
		if (status != PreProcessorResultXMLStatus.NONE)
			throw new SAXException("already have a started element");

		if (arg2.equals(XMLBaliseFormat.BASIC_RESULT))
		{// balise result
			if (result != null)
				throw new SAXException("already have a BASIC RESULT");

			// set les donne pour recupere le basic result
			status = PreProcessorResultXMLStatus.BASIC_RESULT;
			delegate = new BasicResultXMLHandler();

			return true;
		}

		// si pas de result refuse les autres
		if (result == null)
			throw new SAXException("need a BASIC RESULT first");

		if (arg2.equals(XMLBaliseFormat.PREPROC_RESULT_STATUS))
		{// balise status
			status = PreProcessorResultXMLStatus.PREPROC_RESULT_STATUS;
			return true;
		}

		// peut pas commencer balise qui existe pas
		throw new SAXException("unexpected balise: " + arg2);
	}

	/**
	 * Return the result.
	 * 
	 * @return the result
	 */
	public PreProcessorResult getResult() {
		return result;
	}

	/**
	 * Set result status in the {@link PreProcessorResult}.
	 * 
	 * @throws SAXException
	 */
	private void setResultStatus() throws SAXException {
		result.setStatus(PreProcessorResultStatus.getValueFromXML(buffer.toString()));
		if (result.getStatus() == null)
			throw new SAXException("status unknown");
	}
}
